package bandi.osgi.dal.dao.devices.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import bandi.osgi.dal.dao.abstr.AbstractDAO;
import bandi.osgi.dal.dao.beans.Device;
import bandi.osgi.dal.dao.beans.DeviceType;
import bandi.osgi.dal.dao.beans.User;
import bandi.osgi.dal.dao.devices.DeviceDAO;

public class DeviceDAOImpl extends AbstractDAO implements DeviceDAO {
	public DeviceDAOImpl(){}
	@Override
	public void deleteDevice(Device device) {
		final String sql = "DELETE FROM DEVICES WHERE DEVICE_ID = "
				+ device.getDevice_id();
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Exception e) {
			System.err.println("Internal error: "+e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public List<Device> getAllDevices() {
		final String sql = "SELECT * FROM DEVICES";
		Connection connection = null;
		Statement statement = null;
		final List<Device> list = new ArrayList<Device>();
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				DeviceType dt_dummy = new DeviceType();
				dt_dummy.setDevice_type_id(rs.getLong("DEVICE_TYPE_ID"));
				User u_dummy = new User();
				u_dummy.setUser_id(rs.getLong("USER_ID"));
				list.add(new Device(rs.getString("CARRIER"), rs
						.getLong("DEVICE_ID"), dt_dummy, rs
						.getString("PHONE_NUMBER"), u_dummy,rs.getString("Lat"),rs.getString("Lon"), rs.getString("Time")));
			}
			statement.close();
			return list;
		} catch (final Exception e) {
			System.err.println("Internal error: "+e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public Device getDevice(long deviceID) {
		final String sql = "SELECT * FROM DEVICES WHERE DEVICE_ID = "
				+ deviceID;
		Connection connection = null;
		Statement statement = null;
		Device d = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			rs.next();
			DeviceType dt_dummy = new DeviceType();
			dt_dummy.setDevice_type_id(rs.getLong("DEVICE_TYPE_ID"));
			User u_dummy = new User();
			u_dummy.setUser_id(rs.getLong("USER_ID"));
			d = new Device(rs.getString("CARRIER"), rs.getLong("DEVICE_ID"),
					dt_dummy, rs.getString("PHONE_NUMBER"), u_dummy,rs.getString("Lat"),rs.getString("Lon"), rs.getString("Time"));
			statement.close();
			return d;
		} catch (final Exception e) {
			System.err.println("Internal error: "+e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public List<Device> getDevices(User user) {
		final String sql = "SELECT * FROM DEVICES WHERE USER_ID = "
				+ user.getUser_id();
		Connection connection = null;
		Statement statement = null;
		final List<Device> list = new ArrayList<Device>();
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				DeviceType dt_dummy = new DeviceType();
				dt_dummy.setDevice_type_id(rs.getLong("DEVICE_TYPE_ID"));
				list.add(new Device(rs.getString("CARRIER"), rs
						.getLong("DEVICE_ID"), dt_dummy, rs
						.getString("PHONE_NUMBER"), user,rs.getString("Lat"),rs.getString("Lon"), rs.getString("Time")));
			}
			statement.close();
			return list;
		} catch (final Exception e) {
			System.err.println("Internal error: "+e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public List<Device> getDevices(DeviceType deviceType) {
		final String sql = "SELECT * FROM DEVICES WHERE DEVICE_TYPE_ID = "
				+ deviceType.getDevice_type_id();
		Connection connection = null;
		Statement statement = null;
		final List<Device> list = new ArrayList<Device>();
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				User u_dummy = new User();
				u_dummy.setUser_id(rs.getLong("USER_ID"));
				list.add(new Device(rs.getString("CARRIER"), rs
						.getLong("DEVICE_ID"), deviceType, rs
						.getString("PHONE_NUMBER"), u_dummy,rs.getString("Lat"),rs.getString("Lon"), rs.getString("Time")));
			}
			statement.close();
			return list;
		} catch (final Exception e) {
			System.err.println("Internal error: "+e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public void insertDevice(Device device) {
		final String sql = "INSERT INTO DEVICES(DEVICE_ID, USER_ID, DEVICE_TYPE_ID, CARRIER, PHONE_NUMBER,LAT,LON,TIME) "
				+ "VALUES("
				+ null
				+ ", "
				+ device.getUser().getUser_id()
				+ ", "
				+ device.getDevice_type().getDevice_type_id()
				+ ", '"
				+ device.getCarrier() + "', '" + device.getPhone_number() + "', '" + device.getLat() + "', '" + device.getLon() + "', '" + device.getTime() + "')";
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Throwable e) {
			System.err.println("Internal error: "+e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public void updateDevice(Device device) {
		//System.out.println("itt van");
		final String sql = "UPDATE DEVICES SET " + "USER_ID = "
				+ device.getUser().getUser_id() + ", " + "DEVICE_TYPE_ID = "
				+ device.getDevice_type().getDevice_type_id() + ", "
				+ "CARRIER = '" + device.getCarrier() + "', PHONE_NUMBER = '"+ device.getPhone_number() + "',Lat='"+ device.getLat()+ "',Lon='"+ device.getLon()+"',Time='"+device.getTime()+  "' WHERE DEVICE_ID = "
				+ device.getDevice_id();
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Throwable e) {
			System.err.println("Internal error: "+e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

}
